package com.roc.rocketmqconsumer.messageModel;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 发布订阅组-B-消费者-2
 *
 * @author Roker
 * @date 2025/07/09
 */
public class PublishSubscribeGroupBConsumer2 {
    public static void main(String[] args) throws Exception {
        // 1、实例化消费者，指定组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("roc-consumer-group-B");
        // 2、指定NameServer地址
        consumer.setNamesrvAddr("localhost:9876");
        // 3、订阅主题Topic和Tag
        consumer.subscribe("TOPIC_PUBLISH_SUBSCRIBE", "TagA");
        // 4、注册回调实现类来处理从broker拉取回来的消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println("消费者组B-2，接收消息：" + new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        // 5、启动消费者实例
        consumer.start();
        System.out.println("消费者组B-2已启动,监听主题：TOPIC_PUBLISH_SUBSCRIBE，等待接受消息……");
    }
}
